package utils.file;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class FileUtil {
    private static List<Tree> list = new ArrayList<>();//用来存放数据
    private static Integer id = 0;//因为测试使用，当初主键id来用

    /**
     * 将List转成树形
     * @param trees
     * @param parentId
     * @return
     */
    public static List<Tree> treeMenuList(List<Tree> trees, int parentId) {
        List<Tree> childMenu = new ArrayList<>();
        for (Tree tree : trees) {
            int id = tree.getId();
            int pid = tree.getParentId();
            if (parentId == pid) {
                List<Tree> c_node = treeMenuList(trees, id);
                List<Tree> child = tree.getChildren();
                child.addAll(c_node);
                tree.setChildren(child);
                childMenu.add(tree);
            }
        }
        return childMenu;
    }
    
    public static List<Tree> getFileTree(String filepath,String directoryName){
        list = new ArrayList<>();
        id = 0;
        int parentId = 0;  //初始化父节点id
        try {
            file(filepath,parentId,directoryName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void file(String filepath,int parentId,String directoryName) throws FileNotFoundException {
        File file = new File(filepath);
      //1.判断文件
        if(!file.exists()){
            throw new FileNotFoundException("文件不存在"+filepath);
        }
      //2.是文件该怎么执行
        if(file.isFile()){
            String name = file.getName();
            String path = file.getAbsolutePath();
            String relativePath = path.substring(path.indexOf(directoryName));
            Tree tree = new Tree(id++,name,path,parentId,1,relativePath,0,new ArrayList<>());
            list.add(tree);
            return ;
        }
        //3.获取文件夹路径下面的所有文件递归调用；
        if(file.isDirectory()){
            String name = file.getName();
            String path = file.getAbsolutePath();
            String relativePath = path.substring(path.indexOf(directoryName));
            Tree tree = new Tree(id++,name,path,parentId,0,relativePath,0,new ArrayList<>());
            list.add(tree);
            String[] list = file.list();
            String parent = file.getParent();
            for (int i = 0;i<list.length;i++){
                String s = list[i];
                String newFilePath = path+"/"+s;//根据当前文件夹，拼接其下文文件形成新的路径
                file(newFilePath,tree.getId(),directoryName);
            }
        }
    }
    public static boolean deleteFile(File dirFile) {
        // 如果dir对应的文件不存在，则退出
        if (!dirFile.exists()) {
            return false;
        }

        if (dirFile.isFile()) {
            return dirFile.delete();
        } else {
            for (File file : dirFile.listFiles()) {
                deleteFile(file);
            }
        }
        return dirFile.delete();
    }
}